What is highlight-words-core?
The highlight-words-core npm package is a utility for finding and highlighting words within a string. It is particularly useful for search functionalities where you need to emphasize the search terms within the results.
What are highlight-words-core's main functionalities?
Highlight Words
This feature allows you to find and highlight specific words within a given text. The `findAll` function takes an object with `searchWords` and `textToHighlight` properties and returns an array of chunks indicating the positions of the highlighted words.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text to highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight
});
console.log(chunks);
Case Insensitive Search
This feature allows you to perform a case-insensitive search. By setting the `caseSensitive` option to `false`, the search will ignore case differences between the search words and the text.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a Sample text to Highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true,
caseSensitive: false
});
console.log(chunks);
Escape Special Characters
This feature allows you to escape special characters in the search words. By setting the `autoEscape` option to `true`, special characters in the search words will be escaped, preventing them from being interpreted as regular expressions.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text with special characters like * and ?.';
const searchWords = ['*', '?'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true
});
console.log(chunks);
Other packages similar to highlight-words-core
react-highlight-words
The react-highlight-words package provides similar functionality but is specifically designed for React applications. It allows you to highlight words within a React component, making it more suitable for front-end development compared to highlight-words-core, which is a more general utility.
highlight.js
The highlight.js package is a syntax highlighter for code, which can also be used to highlight words within a text. However, it is more focused on code syntax highlighting rather than general text highlighting, making it a bit different in scope compared to highlight-words-core.
mark.js
The mark.js package is a versatile library for highlighting text. It offers more advanced features like custom element creation, regular expression support, and more. It is more feature-rich compared to highlight-words-core, which is simpler and more focused on basic word highlighting.
Utility functions shared by react-highlight-words
and react-native-highlight-words
.
API
The primary API for this package is a function exported as findAll
. This method searches a string of text for a set of search terms and returns an array of "chunks" that describe the matches found.
Each "chunk" is an object consisting of a pair of indices (chunk.start
and chunk.end
) and a boolean specfifying whether the chunk is a match (chunk.highlight
). For example:
import { findAll } from "highlight-words-core";
const textToHighlight = "This is some text to highlight.";
const searchWords = ["This", "i"];
const chunks = findAll({
searchWords,
textToHighlight
});
const highlightedText = chunks
.map(chunk => {
const { end, highlight, start } = chunk;
const text = textToHighlight.substr(start, end - start);
if (highlight) {
return `<mark>${text}</mark>`;
} else {
return text;
}
})
.join("");
Run this example on Code Sandbox.
findAll
The findAll
function accepts several parameters, although only the searchWords
array and textToHighlight
string are required.
Parameter | Required? | Type | Description |
---|
autoEscape | | boolean | Escape special regular expression characters |
caseSensitive | | boolean | Search should be case sensitive |
findChunks | | Function | Custom find function (advanced) |
sanitize | | Function | Custom sanitize function (advanced) |
searchWords | ✅ | Array<string> | Array of words to search for |
textToHighlight | ✅ | string | Text to search and highlight |
License
MIT License - fork, modify and use however you want.